package com.stock.stockana.ui.fragments;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.andexert.library.RippleView;
import com.stock.publiclib.utils.StringUtil;
import com.stock.stockana.R;
import com.stock.stockana.StockCenter;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.GetUserInfoEvent;
import com.stock.stockana.events.network.GetVersionCodeEvent;
import com.stock.stockana.requests.GetUserInfo;
import com.stock.stockana.requests.GetVersionCode;
import com.stock.stockana.ui.activities.VersionNoticeActivity;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * Created by ljgsonx on 2016/3/4.
 */
public class SettingFragment extends Fragment {

    private static final String TAG = "VersionNoticeFragment->";

    TextView mUserPhoneNoTv;
    TextView mUserEmailTv;
    RippleView mMotifyPhoneNoRv;
    RippleView mMotifyEmailRv;
    RippleView mMotifyPwdRv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        GetUserInfo getUserInfo = GetUserInfo.build();
        StockCenter.doReq(getUserInfo);
        View view = inflater.inflate(R.layout.user_setting_layout, container, false);
        mUserPhoneNoTv = (TextView) view.findViewById(R.id.userPhoneNoTv);
        mUserEmailTv = (TextView) view.findViewById(R.id.userEmailTv);
        mMotifyPhoneNoRv = (RippleView) view.findViewById(R.id.motifyPhoneNoRv);
        mMotifyEmailRv = (RippleView) view.findViewById(R.id.motifyEmailRv);
        mMotifyPwdRv = (RippleView) view.findViewById(R.id.motifyPwdRv);

        mMotifyPhoneNoRv.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                String phone = (String) mUserPhoneNoTv.getText();
                MotifyUserNameFragment fragment = new MotifyUserNameFragment();
                Bundle bundle = new Bundle();
                bundle.putString("phoneNo",phone);
                fragment.setArguments(bundle);
                getFragmentManager().beginTransaction()
                        .setCustomAnimations(R.anim.in_from_right,R.anim.out_from_left,
                                R.anim.in_from_left,R.anim.out_from_right)
                        .replace(com.stock.publiclib.R.id.id_baseFrame, fragment)
                                 .addToBackStack(null).commit();
            }
        });
        mMotifyEmailRv.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                Bundle bundle = new Bundle();
                bundle.putInt("userNameType", MotifyUserNameFragment.USERNAME_TYPE_EMAIL);
                MotifyUserNameFragment motifyUserNameFragment = new MotifyUserNameFragment();
                motifyUserNameFragment.setArguments(bundle);
                getFragmentManager().beginTransaction()
                        .setCustomAnimations(R.anim.in_from_right,R.anim.out_from_left,
                                R.anim.in_from_left,R.anim.out_from_right)
                        .replace(com.stock.publiclib.R.id.id_baseFrame, motifyUserNameFragment)
                        .addToBackStack(null).commit();
            }
        });
        mMotifyPwdRv.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                getFragmentManager().beginTransaction()
                        .setCustomAnimations(R.anim.in_from_right,R.anim.out_from_left,
                                R.anim.in_from_left,R.anim.out_from_right)
                        .replace(com.stock.publiclib.R.id.id_baseFrame, new MotifyUserPwdFragment())
                        .addToBackStack(null).commit();
            }
        });
        return view;
    }

    @Subscribe
    public void onEventMainThread(GetUserInfoEvent event) {
        if (event.getResponseCode() == NetWorkConstants.RESPONSE_CODE_OK) {
            if(!StringUtil.isBlank(event.getsUser().getMobilephone())){
                mUserPhoneNoTv.setText(event.getsUser().getMobilephone());
            }else{
                mUserPhoneNoTv.setText("无");
            }
            if(!StringUtil.isBlank(event.getsUser().getMobilephone())){
                mUserEmailTv.setText(event.getsUser().getEmail());
            }else{
                mUserEmailTv.setText("无");
            }
        }else {
            Toast.makeText(getActivity(), "获取用户信息失败,请重试!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }
}
